var express = require("express");
var app = express();
app.use(express.static('./static'));

var router = express.Router();
app.use(router);

var data = [
{id:1 ,name: '第一个商品', price:'12'},
{id:2 ,name: '第二个商品', price:'18'},
{id:3 ,name: '第三个商品', price:'20'}
]

router.get('/', function (req, res, next) {
	req.url = 'static/index.html';
	next();
});
router.get('/api/product', function(req, res) {
   res.json({
		errno: 0,
		data: data
	});
})
router.get('/api/product/:id', function(req, res) {
	var id = req.params.id
	console.log('id='+id)
	var product = data.filter( (item) => item.id == id)
	res.json({
		errno:0,
		data: product
	})
 })
app.listen(3000, '127.0.0.1');
console.log("3000端口启动了");
